package com.sunday.haowushop.ui.order;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.haowushop.R;
import com.sunday.haowushop.entity.GroupBuyOrder;
import com.sunday.haowushop.http.ApiClient;
import com.sunday.haowushop.ui.WebViewActivity;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by 刘涛 on 2017/6/4.
 */

public class GroupOrderDetailActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.order_status)
    TextView orderStatus;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.user_addr)
    TextView userAddr;
    @Bind(R.id.shop_name)
    TextView shopName;
    @Bind(R.id.product_image)
    ImageView productImage;
    @Bind(R.id.product_name)
    TextView productName;
    @Bind(R.id.product_price)
    TextView productPrice;
    @Bind(R.id.product_params)
    TextView productParams;
    @Bind(R.id.group_num)
    TextView groupNum;
    @Bind(R.id.group_price)
    TextView groupPrice;
    @Bind(R.id.text_sales)
    TextView textSales;
    @Bind(R.id.product_num)
    TextView productNum;
    @Bind(R.id.pro_layout)
    RelativeLayout proLayout;
    @Bind(R.id.express_money)
    TextView expressMoney;
    @Bind(R.id.total_money)
    TextView totalMoney;
    @Bind(R.id.order_no)
    TextView orderNo;
    @Bind(R.id.order_create_time)
    TextView orderCreateTime;
    @Bind(R.id.order_pay_time)
    TextView orderPayTime;
    @Bind(R.id.order_send_time)
    TextView orderSendTime;
    @Bind(R.id.order_btn1)
    TextView orderBtn1;
    @Bind(R.id.order_btn2)
    TextView orderBtn2;
    @Bind(R.id.btn_layout)
    RelativeLayout btnLayout;
    @Bind(R.id.order_btn3)
    TextView orderBtn3;

    private GroupBuyOrder order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_order_detail);
        ButterKnife.bind(this);
        order = (GroupBuyOrder) getIntent().getSerializableExtra("groupOrder");
        titleView.setText("订单详情");
        initView();
    }

    private void initView() {
        if (order == null) {
            return;
        }
        switch (order.getStatus()) {
            case 3:
                orderStatus.setText("拼团失败");
                btnLayout.setVisibility(View.GONE);
                break;
            case 1:
                orderStatus.setText("待成团");
                btnLayout.setVisibility(View.GONE);
                break;
            case 2:
                orderStatus.setText("已成团");
                if (order.getStatus() == 2) {
                    btnLayout.setVisibility(View.VISIBLE);
                    orderBtn1.setText("确认收货");
                    orderBtn2.setVisibility(View.VISIBLE);
                    orderBtn2.setText("查看物流");
                    orderBtn3.setVisibility(View.VISIBLE);
                    orderBtn3.setText("申请退货");
                } else if (order.getStatus() == 5) {
                    btnLayout.setVisibility(View.VISIBLE);
                    orderBtn1.setText("退货详情");
                    orderBtn2.setVisibility(View.GONE);
                    orderBtn3.setVisibility(View.GONE);
                } else {
                    btnLayout.setVisibility(View.GONE);
                }
                break;
            default:btnLayout.setVisibility(View.GONE);
                orderStatus.setText("拼团失败");
        }
        if (!TextUtils.isEmpty(order.getProductImage())) {
            Glide.with(mContext)
                    .load(order.getProductImage())
                    .error(R.mipmap.ic_default)
                    .into(productImage);
        }
        productName.setText(order.getProductName());
        productParams.setText(order.getElements());
        productPrice.setText(String.format("¥%s", order.getTotalFee().setScale(2, RoundingMode.HALF_UP)));
        productNum.setText("X1");

        userName.setText(String.format("%1$s  %2$s", order.getName(), order.getMobile()));
        userAddr.setText(order.getAddress());
        shopName.setText(order.getShopName());
//        expressMoney.setText(String.format("¥%s",order.getExpressFee().setScale(2, RoundingMode.HALF_UP)));
        totalMoney.setText(String.format("¥%s", order.getTotalFee().setScale(2, RoundingMode.HALF_UP)));
        orderNo.setText(order.getOrderNo());
        orderCreateTime.setText(order.getCreateTime());
        orderPayTime.setText(order.getPayTime());
        orderSendTime.setText(order.getSendTime());
    }


    @OnClick({R.id.order_btn1, R.id.order_btn2, R.id.order_btn3})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_btn1:
                //确认收货
                if (order.getStatus() == 2) {
                    confirmRecieve();
                } else if (order.getStatus() == 5) {
                    intent = new Intent(mContext, GroupRefundDetailActivity.class);
                    intent.putExtra("order", order);
                    startActivity(intent);
                }
                break;
            case R.id.order_btn2:
                //查看物流
                if (order.getStatus() == 2) {
                    intent = new Intent(mContext, WebViewActivity.class);
                    String url = String.format("https://m.kuaidi100.com/index_all.html?type=%1$s&postid=%2$s", order.getExpressEng(), order.getExpressNo());
                    intent.putExtra("url", url);
                    intent.putExtra("title", "物流查询");
                    startActivity(intent);
                }
            case R.id.order_btn3:
                //申请退货
                if (order.getStatus() == 2) {
                    intent = new Intent(mContext, GroupOrderApplyRefundActivity.class);
                    intent.putExtra("order", order);
                    startActivity(intent);
                }
                break;
        }
    }

    private void confirmRecieve() {
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().confirmGroupRecieve(order.getId());
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish) {
                    return;
                }
                dissMissDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
